package com.wwp.tbshop.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.CollectListBean;
import com.wwp.tbshop.presenter.CollectListPresenter;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.view.adapter.CollectAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Xiaokun on 2017/6/17.
 */

public class CollectGoodsActivity extends BaseActivity {

    @Bind(R.id.iv_collect_back)
    ImageView ivCollectBack;
    @Bind(R.id.rv_collect_listview)
    PullToRefreshListView rvCollectListview;
    private CollectListPresenter mPresenter;
    private CollectAdapter mCollectAdapter;
    private List<CollectListBean.ProductListBean> mProductList = new ArrayList<CollectListBean.ProductListBean>();
    private int page = 1;

    //TODO:根据获取用户的userId

    private String mUserId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void init() {
        super.init();
        mUserId = SPUtils.getUerid();
        mPresenter = new CollectListPresenter(this);
        mCollectAdapter = new CollectAdapter(this);
        rvCollectListview.setAdapter(mCollectAdapter);
        rvCollectListview.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        rvCollectListview.setOnRefreshListener(refreshListener);
        mPresenter.getCollectList(page, "10", mUserId);
        rvCollectListview.setOnItemClickListener(itemClickListener);
    }

    private AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(UiUtil.getContext(),mProductList.get(position-1).getId()+"",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(UiUtil.getContext(),ProductDetailActivity.class);
            intent.putExtra("pId",mProductList.get(position-1).getId());
            startActivity(intent);
        }
    };



    @OnClick(R.id.iv_collect_back)
    public void onViewClicked() {
        finish();
    }

    public void onSuccess(CollectListBean collectListBean) {
        if (collectListBean.getListCount() == 0) {
            Toast.makeText(this, "没有更多", Toast.LENGTH_SHORT).show();
            rvCollectListview.setMode(PullToRefreshBase.Mode.DISABLED);
            return;
        }
        mProductList.addAll(collectListBean.getProductList());
        mCollectAdapter.setDatas(mProductList);
        mCollectAdapter.notifyDataSetChanged();

    }

    private PullToRefreshBase.OnRefreshListener refreshListener = new PullToRefreshBase.OnRefreshListener() {
        @Override
        public void onRefresh(PullToRefreshBase pullToRefreshBase) {

            rvCollectListview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    page++;
                    Log.e("page", "run: " + page);
                    mPresenter.getCollectList(page, "10", mUserId);
                    rvCollectListview.onRefreshComplete();
                }
            }, 1000);
        }
    };


}
